package dao;

import controller.AdminServlet;
import model.MemberAdmin;
import model.MemberUser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class MemberAdminDao {
    final String admin_user_table = "member_admin";
    public static final Logger logger = LogManager.getLogger(MemberAdminDao.class);

    /**
     * 用户登录函数
     *
     * @param memberAdmin 管理账号密码登录信息
     * @return boolean 登录成功返回true，失败则false
     * @see SQLException
     */
    public boolean Login(MemberAdmin memberAdmin) throws SQLException {
        Database database = new Database();
        String username = memberAdmin.getUsername();
        String password = memberAdmin.getPassword();
        try (ResultSet resultSet = database.query("select * from " + admin_user_table + " where username ='" + username + "' and password ='" + password + "'")) {
            try {
                if (resultSet.next()) {
                    logger.info("Now admin " + username + " is login.");
                    return true;
                }
            } catch (SQLException e) {
                logger.error(e);
                throw new RuntimeException(e);
            }
        }
        return false;
    }

}
